using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyHunter.Interfaces;

using WeifenLuo.WinFormsUI.Docking;

namespace MyHunter.Contents
{
    public partial class CollectionsExplorer : DockContent
    {
        #region MEMBERS
        private ICollectionManager CollMgr;
        #endregion

        #region CTOR
        public CollectionsExplorer(ICollectionManager colmgr)
        {
            InitializeComponent();
            FillCollectionTree();
            CollMgr = colmgr;
        }
        #endregion

        #region TREE
        private TreeNode GetSiteNode(string site)
        {
            foreach(TreeNode n in this.treeViewCollections.Nodes)
            {
                if(n.Text == site && n.Parent == null)
                    return n;
            }
            return null;
        }

        private void FillCollectionTree()
        {
            TreeNode n;
            DataView v = new DataView(DataSchema.Settings.SettingsMgr.Settings.Sites,"","Name", DataViewRowState.CurrentRows);

            //remove unused site
            foreach(TreeNode node2rem in this.treeViewCollections.Nodes)
            {
                if(DataSchema.Settings.SettingsMgr.Settings.ContainsSite(node2rem.Text) == false)
                    node2rem.Remove();
            }

            foreach(DataRowView srv in v)
            {
                DataSchema.Settings.SettingSchema.SitesRow sr = (DataSchema.Settings.SettingSchema.SitesRow)srv.Row;
                
                bool alreadyExists = false;

                foreach(TreeNode node in this.treeViewCollections.Nodes)
                {
                    if(node.Text == sr.Name)
                    {
                        alreadyExists = true;
                        break;
                    }
                }
                if(alreadyExists)
                    continue;

                n = AppendSite(sr);

                foreach(DataSchema.Settings.SettingSchema.MyCollectionRow mr in ((DataSchema.Settings.SettingSchema)sr.Table.DataSet).GetSiteCollections(sr.Name))
                {
                    AppendCollection(mr, n);
                }
            }
        }

        private TreeNode AppendSite(DataSchema.Settings.SettingSchema.SitesRow sr)
        {
            TreeNode n = new TreeNode();
            n.ImageIndex = 0;
            n.SelectedImageIndex = 1;
            n.Text = sr.Name;
            n.Tag = sr;
            this.treeViewCollections.Nodes.Add(n);
            return n;
        }

        private void AppendCollection(DataSchema.Settings.SettingSchema.MyCollectionRow mr, TreeNode p)
        {
            TreeNode n = new TreeNode();
            n.ImageIndex = 2;
            n.SelectedImageIndex = 3;
            n.Text = mr.Name;
            n.Tag = mr.Path;
            p.Nodes.Add(n);
        }

        #endregion

        #region EVENTS
        private void OnItemDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            try
            {
                if(e.Node.Parent != null)
                {
                    if(CollMgr.ShowCollection(e.Node.Text) == false)
                    {
                        CollMgr.OpenCollection(e.Node.Tag.ToString());
                    }
                }
            }
            catch(Exception )
            {
                DialogResult r = HunterForm.ShowMsg("Collection can't be opened! Would you like to remove it from MyCollections?", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
                if(r == DialogResult.Yes)
                {
                    if(DataSchema.Settings.SettingsMgr.RemoveFromMyCollection(e.Node.Text))
                    {
                        e.Node.Remove();
                    }
                }
            }
        }
        #endregion

        #region PUBLIC
        public void UpdateMyCollection(Interfaces.ICollection c)
        {
            DataSchema.Settings.SettingSchema.SitesRow sr =
                DataSchema.Settings.SettingsMgr.UpdateSite(c.CollectionSite);
            if (sr != null)
            {
                this.AppendSite(sr);
            }
            DataSchema.Settings.SettingSchema.MyCollectionRow mr =
                DataSchema.Settings.SettingsMgr.UpdateMyCollection
                (c.CollectionName, c.CollectionSite, c.CollectionPath);
            if (mr != null)//new item was created
            {
                TreeNode sitenode = GetSiteNode(c.CollectionSite);
                if (sitenode != null)
                    AppendCollection(mr, sitenode);
            }
        }
        #endregion

        #region TOOLBAR HANDLERS
        private void toolStripButtonAddSite_Click(object sender, EventArgs e)
        {
            MyHunter.New.NewSite ns = new MyHunter.New.NewSite(EditMode.New, DataSchema.Settings.SettingsMgr.Settings.Sites);
            if(ns.ShowDialog() == DialogResult.OK)
            {
//                DataSchema.Settings.SettingSchema.SitesRow sr = 
  //                  DataSchema.Settings.SettingsMgr.NewSite(ns.SiteName, ns.SiteDescription);
                string s = ns.SiteIcon;
                //TreeNode n = this.AppendSite(sr);
                FillCollectionTree();
            }
        }

        private void toolStripEdit_Click(object sender, EventArgs e)
        {
            if(this.treeViewCollections.SelectedNode == null
                || this.treeViewCollections.SelectedNode.Parent != null)
            {
                HunterForm.ShowMsg("Select site to edition", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            MyHunter.New.NewSite ns = new MyHunter.New.NewSite(EditMode.Edit, this.treeViewCollections.SelectedNode.Tag);
            if(ns.ShowDialog() == DialogResult.OK)
            {
                FillCollectionTree();
            }
        }

        private void toolStripButtonRemoveSite_Click(object sender, EventArgs e)
        {
            TreeNode n = this.treeViewCollections.SelectedNode;
            if(n != null && n.Parent == null)
            {
                if(n.Nodes.Count == 0)
                {
                    if(DataSchema.Settings.SettingsMgr.DeleteSite(n.Text))
                    {
                        n.Remove();
                    }
                }
                else
                    HunterForm.ShowMsg("This Site still has collections", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                HunterForm.ShowMsg("Select Site to remove", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        #endregion


    }
}